package com.example.library;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputLayout;

public class RegisterActivity extends AppCompatActivity {
    private EditText etName, etId, etPassword, etContact;
    private AutoCompleteTextView spUserType;
    private MaterialButton btnRegister;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etName = findViewById(R.id.et_name);
        etId = findViewById(R.id.et_id);
        etPassword = findViewById(R.id.et_password);
        etContact = findViewById(R.id.et_contact);
        spUserType = findViewById(R.id.sp_user_type);
        btnRegister = findViewById(R.id.btn_register);
        progressBar = findViewById(R.id.progress_bar);

        // 设置用户类型下拉列表
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.user_types, android.R.layout.simple_dropdown_item_1line);
        spUserType.setAdapter(adapter);
        spUserType.setText(adapter.getItem(0).toString(), false); // 默认选择第一项（学生）

        btnRegister.setOnClickListener(v -> {
            String name = etName.getText().toString().trim();
            String id = etId.getText().toString().trim();
            String password = etPassword.getText().toString();
            String contact = etContact.getText().toString().trim();
            String userType = spUserType.getText().toString();
            
            // 输入验证
            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(id) || TextUtils.isEmpty(password)) {
                Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
                return;
            }
            
            // 验证账号格式
            if (!id.matches("^[a-zA-Z0-9]{4,20}$")) {
                Toast.makeText(this, "账号必须是4-20位字母或数字", Toast.LENGTH_SHORT).show();
                return;
            }

            // 验证密码格式
            if (!password.matches("^[a-zA-Z0-9]{6,20}$")) {
                Toast.makeText(this, "密码必须是6-20位字母或数字", Toast.LENGTH_SHORT).show();
                return;
            }

            // 验证姓名格式
            if (!name.matches("^[\\u4E00-\\u9FA5A-Za-z0-9]{2,10}$")) {
                Toast.makeText(this, "姓名必须是2-10位中文、英文或数字", Toast.LENGTH_SHORT).show();
                return;
            }

            // 如果有联系方式，验证格式
            if (!TextUtils.isEmpty(contact) && !contact.matches("^[0-9]{11}$")) {
                Toast.makeText(this, "联系方式必须是11位数字", Toast.LENGTH_SHORT).show();
                return;
            }
            
            // 显示加载进度条
            progressBar.setVisibility(View.VISIBLE);
            btnRegister.setEnabled(false);

            // 调用云端API进行注册
            ApiService.getInstance().register(name, id, password, contact, userType, new ApiService.Callback() {
                @Override
                public void onSuccess(String result) {
                    runOnUiThread(() -> {
                        // 注册成功后写入本地数据库
                        UserDao userDao = new UserDao(RegisterActivity.this);
                        userDao.register(name, id, password, contact, userType);

                        progressBar.setVisibility(View.GONE);
                        btnRegister.setEnabled(true);
                        Toast.makeText(RegisterActivity.this, "注册成功，请登录", Toast.LENGTH_SHORT).show();
                        finish();
                    });
                }

                @Override
                public void onFailure(String error) {
                    runOnUiThread(() -> {
                        progressBar.setVisibility(View.GONE);
                        btnRegister.setEnabled(true);
                        Toast.makeText(RegisterActivity.this, "注册失败: " + error, Toast.LENGTH_SHORT).show();
                    });
                }
            });
        });
    }
} 